﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using Memcached.ClientLibrary;

/// <summary>
///MemcachedTools 的摘要说明
/// </summary>
namespace NewMemcached
{
    public class MemcachedTools
    {

       public MemcachedTools(string server)//服务器IP及端口配置
        {
            String[] serverlist = server.Split(',');
            //String[] serverlist = { "172.28.1.200:11211", "172.28.1.201:11211" };
            SockIOPool pool = SockIOPool.GetInstance("news");
            pool.SetServers(serverlist);
            pool.Initialize();
        }

        private  MemcachedClient NewsClient
        {
            get
            {
                MemcachedClient mc = new MemcachedClient();
                mc.DefaultEncoding = "utf-8";
                mc.EnableCompression = false;
                mc.PrimitiveAsString = true;
                mc.PoolName = "news";
                return mc;
            }
        }

        /// <summary>
        /// 添加一个缓存
        /// </summary>
        /// <param name="obj">缓存对象</param>
        /// <param name="sj">缓存时间</param>
        public  void Add(string key, object obj, DateTime expiry)
        {
            if (!NewsClient.Add(key, obj, expiry))
            {
                NewsClient.Set(key, obj, expiry);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public  object Get(string key)
        {
            return NewsClient.Get(key);
        }
    }
}